<?php

namespace App\Repository\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;

/**
 * Class OrderProducts.
 *
 * @package namespace App\Repository\Models;
 */
class OrderProducts extends Model implements Transformable
{
    use TransformableTrait;
    use SoftDeletes;

    protected $table = 'order_products';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['user_id', 'order_code', 'product_id', 'sku_id', 'product_name', 'model', 'brand_name', 'shop_id',
        'shop_name', 'product_img_url', 'product_price', 'discount_money', 'quantity', 'product_money', 'attributes', 'remark',
        'freight', 'is_appraise'];

    const IS_APPRAISE_YES = 1;
    const IS_APPRAISE_NO = 0;

    //修改买家评价状态
    public function getAttributesAttribute()
    {
        if (strstr($this->attributes['attributes'], ','))
        {
            $temp = array_filter(explode(',',$this->attributes['attributes']));

            foreach ($temp as $k=>&$v){
                $v = array_filter(explode(':',$v));
            }
            return $temp;
        }else{
            return $this->attributes['attributes'];
        }
    }
    public function product()
    {
        return $this->hasOne(Products::class, 'id', 'product_id');
    }

    public function sku()
    {
        return $this->hasOne(ProductSkus::class, 'id', 'sku_id');
    }

}
